# 社区子系统
from sqlalchemy import Column, Integer, String, ForeignKey
from sqlalchemy.orm import relationship, backref
from utils.dataBase import Base


class Article(Base):
    __tablename__ = "article"
    aid = Column(Integer, unique=True, primary_key=True)
    type = Column(String(0), unique=False)
    title = Column(String(0), unique=False)
    fromid = Column(String(0), ForeignKey('user.uid'))
    content = Column(String(0), unique=False)
    time = Column(String(0), unique=False)
    like = Column(Integer, unique=False)

    user = relationship('User', backref=backref('article', lazy=True))

    def __init__(self, type, title, fromid, time, content, like=0):
        self.type = type
        self.title = title
        self.author = fromid
        self.time = time
        self.content = content
        self.like = like

    def __repr__(self):
        return '<Article %r>' % self.aid


class Comment(Base):
    __tablename__ = "comment"
    cid = Column(Integer, unique=True, primary_key=True)
    replyid = Column(Integer, ForeignKey('article.aid'), ForeignKey('help.hid'))
    uid = Column(Integer, ForeignKey('user.uid'))
    content = Column(String, unique=False)
    time = Column(String, unique=False)
    type = Column(String, unique=False)

    article = relationship('Article', backref=backref('comment', lazy=True))
    user = relationship('User', backref=backref('comment', lazy=True))
    help = relationship('Help', backref=backref('comment', lazy=True))

    def __init__(self, replyid, uid, content, time, type):
        self.replyid = replyid
        self.uid = uid
        self.content = content
        self.time = time
        self.type = type

    def __repr__(self):
        return '<Comment %r>' % self.cid


class CommentReply(Base):
    __tablename__ = "comment_reply"
    cid = Column(Integer, ForeignKey('comment.cid'), primary_key=True)
    fromid_uid = Column(Integer, ForeignKey('user.uid'))
    toid_uid = Column(Integer, ForeignKey('user.uid'))
    content = Column(String, unique=False)
    time = Column(String, unique=False)

    comment = relationship('Comment', backref=backref('comment_reply', lazy=True))
    fromid = relationship('User', backref=backref('comment_reply_fromid', lazy=True), foreign_keys=[fromid_uid])
    toid = relationship('User', backref=backref('comment_reply_toid', lazy=True), foreign_keys=[toid_uid])

    def __init__(self, cid, fromid, toid, content, time):
        self.cid = cid
        self.fromid = fromid
        self.toid = toid
        self.content = content
        self.time = time

    def __repr__(self):
        return '<CommentReply %r>' % self.cid


class Help(Base):
    __tablename__ = "help"
    hid = Column(Integer, unique=True, primary_key=True)
    title = Column(String, unique=False)
    content = Column(String, unique=False)
    time = Column(String, unique=False)
    fromid = Column(Integer, ForeignKey('user.uid'))
    toid = Column(Integer)

    user = relationship('User', backref=backref('comment_reply', lazy=True))

    def __init__(self, title, content, time, fromid, toid):
        self.title = title
        self.content = content
        self.time = time
        self.fromid = fromid
        self.toid = toid

    def __repr__(self):
        return '<Help %r>' % self.title


class Footstep(Base):
    __tablename__ = "footstep"
    fid = Column(Integer, unique=True, primary_key=True)
    uid = Column(Integer, ForeignKey('user.uid'))
    aid = Column(Integer, ForeignKey('article.aid'))
    time = Column(String)

    user = relationship('User', backref=backref('footstep', lazy=True))
    article = relationship('Article', backref=backref('footstep', lazy=True))

    def __init__(self, uid, aid, time):
        self.uid = uid
        self.aid = aid
        self.time = time

    def __repr__(self):
        return '<Footstep %r>' % self.fid
